<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/9 0009
 * Time: 下午 5:02
 */
namespace app\admin\model;
use think\console\command\make\Model;
use think\Db;

class Index extends Model{
    #全部文章
    public function index($pid=''){
        $compose = Db::name('compose')->where(['del'=>1,'pid'=>$pid,'status'=>1])->select();
        foreach ($compose as $k =>$v) {
            $compose[$k]['a'] = dmy($v['createtime']);#日期 格式09 Sept 2018
            $compose[$k]['b'] = dm($v['createtime']);#格式 20 sept
            $compose[$k]['short_content'] = short_content($v['content']);#截取文章
            $compose[$k]['comment'] = Db::name('comment')
                ->where(['del' => 1, 'compose_id' => $v['id']])
                ->count();
            $compose[$k]['likes'] = Db::name('likes')
                ->where(['status'=>1,'type'=>1,'type_id'=>$v['id']])
                ->count();
        }
        return $compose;
    }
    #点赞,评论
    public function like_com($id){
        $composes = Db::name('compose')->where(['userid'=>$id['id'],'del'=>1])->select();
        $likes =[];$comment=[];
        foreach ($composes as $item => $v) {
            $likes = Db::name('likes')->where(['type'=>1,'type_id'=>$v['id'],'status'=>1])->select();
            $comment = Db::name('comment')->where(['compose_id'=>$v['id'],'del'=>1])->select();
        }
        $like =count($likes);
        $com = count($comment);
        $res =[
            'like'=>$like,
            'comment'=>$com
        ];
        return $res;
    }
}